﻿using System;
using System.Collections.Generic;
using Go.Tools;
using System.Text;
using System.Threading;


namespace Go.Model
{
    public class Game: IObservable
    {
        Player _firstPlayer;
        Dictionary<Coordinate, Position> _board;
        RulesValidator _goRulesValidator;

        public Game(Dictionary<Coordinate, Position> board, Player player)
        {
            _board = board;
            _firstPlayer = player;
            _goRulesValidator = RulesValidator.instance(board, players());
        }

        // Devuelve el Jugador ganador en caso de empate devuelve null
        public Player play() {
            bool isFinish = false;
            Player aPlayer = _firstPlayer;
            
            while (!isFinish)
            {

                aPlayer.play();
                if (_goRulesValidator.isFinishedNobodyWantPlay(aPlayer))
                {
                    aPlayer = calculateWinByPoints();
                    NotifyObservers(aPlayer);
                    return aPlayer;
                }
                else if (_goRulesValidator.isFinishedAPlayerWin(aPlayer))
                {
                    NotifyObservers(aPlayer);
                    return aPlayer;
                }
                aPlayer = aPlayer.Opponent;
                Thread.Sleep(ConstantThread.SleepTimeGame);
            }
            
            return null;
        }

        //Calcula quien es el ganador por puntos y lo devuelve
        public Player calculateWinByPoints() {
            return null;
        }

        public Dictionary<Coordinate, Position> Board
        {
            get { return _board; }
            set { _board = value; }
        }

        public List<Player> players() {
            List<Player> players = new List<Player>();
            players.Add(_firstPlayer);
            players.Add(_firstPlayer.Opponent);
            return players;
        }


    }
}
